using HarmonyLib;
using System;
using System.Reflection;

namespace CameraChange
{
    [HarmonyPatch(typeof(GameCamera), "Start")]
    public class GameCameraStartPatch
    {
        [Config(Description = "基础相机偏移", MinValue = 1f, MaxValue = 10f, ConfigName = "defaultAimOffset")]
        private static float _defaultAimOffset = 8f;

        [Config(Description = "瞄准偏移系数", MinValue = 0.1f, MaxValue = 2f, FloatDecimalPlaces = 2,
            ConfigName = "defaultAimOffsetDistanceFactor")]
        private static float _defaultAimOffsetDistanceFactor = 0.8f;

        [Config(Description = "相机移动速度", MinValue = 5f, MaxValue = 20f, ConfigName = "lerpSpeed")]
        private static float _lerpSpeed = 12f;

        [Config(Description = "默认视野范围", MinValue = 15f, MaxValue = 30f, ConfigName = "defaultFOV")]
        private static float _defaultFOV = 20f;

        [Config(Description = "瞄准视野范围", MinValue = 10f, MaxValue = 25f, ConfigName = "adsFOV")]
        private static float _adsFOV = 15f;

        // 游戏相机实例 
        private static GameCamera? _gameCamera;

        [Reflection(typeof(GameCamera), "defaultAimOffset")]
        private static readonly FieldInfo? DefaultAimOffsetField = null;

        [Reflection(typeof(GameCamera), "defaultAimOffsetDistanceFactor")]
        private static readonly FieldInfo? DefaultAimOffsetDistanceFactorField = null;

        [Reflection(typeof(GameCamera), "lerpSpeed")]
        private static readonly FieldInfo? LerpSpeedField = null;

        [Reflection(typeof(GameCamera), "defaultFOV")]
        private static readonly FieldInfo? DefaultFOVField = null;

        [Reflection(typeof(GameCamera), "adsFOV")]
        private static readonly FieldInfo? AdsFOVField = null;

        // 保存的原始值
        private static float? _rawDefaultAimOffset;
        private static float? _rawDefaultAimOffsetDistance;
        private static float? _rawLerpSpeed;
        private static float? _rawDefaultFOV;
        private static float? _rawAdsFOV;
        
        [HarmonyPostfix]
        // ReSharper disable once InconsistentNaming
        public static void Postfix(GameCamera __instance)
        {
            try
            {
                _gameCamera = __instance;
                SaveAll();
                SetAll();
            }
            catch (Exception ex)
            {
                ModLog.Error($"Postfix中出错: {ex.Message}");
            }
        }

        private static void SaveAll()
        {
            _rawDefaultAimOffset = (float?)DefaultAimOffsetField?.GetValue(_gameCamera);
            ModLog.Info($"已保存DefaultAimOffset的原始值:{_rawDefaultAimOffset}");
            
            _rawDefaultAimOffsetDistance = (float?)DefaultAimOffsetDistanceFactorField?.GetValue(_gameCamera);
            ModLog.Info($"已保存DefaultAimOffsetDistanceFactor的原始值:{_rawDefaultAimOffsetDistance}");
            
            _rawLerpSpeed = (float?)LerpSpeedField?.GetValue(_gameCamera);
            ModLog.Info($"已保存LerpSpeed的原始值:{_rawLerpSpeed}");
            
            _rawDefaultFOV = (float?)DefaultFOVField?.GetValue(_gameCamera);
            ModLog.Info($"已保存DefaultFOV的原始值:{_rawDefaultFOV}");
            
            _rawAdsFOV = (float?)AdsFOVField?.GetValue(_gameCamera);
            ModLog.Info($"已保存AdsFOV的原始值:{_rawAdsFOV}");
        }

        /// <summary>
        /// 设置相机偏移参数
        /// </summary>
        private static void SetAll()
        {
            if (!_gameCamera)
                return;

            try
            {
                SetDefaultAimOffset();
                SetDefaultAimOffsetDistanceFactor();
                SetLerpSpeed();
                SetDefaultFOV();
                SetAdsFOV();
            }
            catch (Exception ex)
            {
                ModLog.Error($"设置失败：{ex.Message}");
            }
        }

        /// <summary>
        /// 设置默认瞄准偏移
        /// </summary>
        [OnChange("defaultAimOffset")]
        private static void SetDefaultAimOffset()
        {
            if (DefaultAimOffsetField != null && _gameCamera)
            {
                DefaultAimOffsetField.SetValue(_gameCamera, _defaultAimOffset);
                ModLog.Info($"已设置defaultAimOffset: {_defaultAimOffset}");
            }
            else
            {
                ModLog.Warning("defaultAimOffsetField反射未缓存成功");
            }
        }

        /// <summary>
        /// 设置默认瞄准偏移距离因子
        /// </summary>
        [OnChange("defaultAimOffsetDistanceFactor")]
        private static void SetDefaultAimOffsetDistanceFactor()
        {
            if (DefaultAimOffsetDistanceFactorField != null && _gameCamera)
            {
                DefaultAimOffsetDistanceFactorField.SetValue(_gameCamera, _defaultAimOffsetDistanceFactor);
                ModLog.Info($"已设置defaultAimOffsetDistanceFactor: {_defaultAimOffsetDistanceFactor}");
            }
            else
            {
                ModLog.Warning("defaultAimOffsetDistanceFactorField反射未缓存成功");
            }
        }

        /// <summary>
        /// 设置相机插值速度
        /// </summary>
        [OnChange("lerpSpeed")]
        private static void SetLerpSpeed()
        {
            if (LerpSpeedField != null && _gameCamera)
            {
                LerpSpeedField.SetValue(_gameCamera, _lerpSpeed);
                ModLog.Info($"已设置lerpSpeed: {_lerpSpeed}");
            }
            else
            {
                ModLog.Warning("lerpSpeedField反射未缓存成功");
            }
        }

        /// <summary>
        /// 设置默认视野角度
        /// </summary>
        [OnChange("defaultFOV")]
        private static void SetDefaultFOV()
        {
            if (DefaultFOVField != null && _gameCamera)
            {
                DefaultFOVField.SetValue(_gameCamera, _defaultFOV);
                ModLog.Info($"已设置defaultFOV: {_defaultFOV}");
            }
            else
            {
                ModLog.Warning("defaultFOVField反射未缓存成功");
            }
        }

        /// <summary>
        /// 设置瞄准视野角度
        /// </summary>
        [OnChange("adsFOV")]
        private static void SetAdsFOV()
        {
            if (AdsFOVField != null && _gameCamera)
            {
                AdsFOVField.SetValue(_gameCamera, _adsFOV);
                ModLog.Info($"已设置adsFOV: {_adsFOV}");
            }
            else
            {
                ModLog.Warning("adsFOVField反射未缓存成功");
            }
        }

        [Button("恢复所有设置", ButtonText = "重置")]
        // ReSharper disable once UnusedMember.Local
        private static void ResetAll()
        {
            // 取消所有等待中的修改回调
            AutoConfigManager.CancelAllDelayedCallback();

            _defaultAimOffset = _rawDefaultAimOffset ?? 5f;
            _defaultAimOffsetDistanceFactor = _rawDefaultAimOffsetDistance ?? 0.5f;
            _lerpSpeed = _rawLerpSpeed ?? 12f;
            _defaultFOV = _rawDefaultFOV ?? 20f;
            _adsFOV = _rawAdsFOV ?? 15f;

            AutoConfigManager.SaveAllConfigs();

            if (_gameCamera) SetAll();
        }
    }
}